var express = require('express');
var router = express.Router();

const Query=require('../utils/db.js')
const { api_url }=require('../utils/config.js')



/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express123' });
});

//轮播图接口
router.get('/banners',async(req,res,next)=>{
  let sql=`SELECT id,title,isshow, CONCAT('${api_url}','/uploads/banners/',image_src) AS image_src FROM e_banners WHERE isshow=1`

  
  let {err,result}=await Query(sql)
  if(err){
    next({
      msg:'稍后再试',
      status:500
    })
    return
  }

  let data={
    msg:'OK',
    status:200,
    result:result
  }
  res.send(data)
})

//课程列表
router.get('/courseinfo',async(req,res,next)=>{
  let {type=1} = req.query
  let size=type==1 ? 8 : 6
  let sql = `SELECT id,cid,type_name,CONCAT('${api_url}','/uploads/courses/',image_src) AS image_src,subject_name,title,area_name,price,type FROM e_course WHERE type = ${type}  ORDER BY id DESC LIMIT ${size}`

  let result=await Query(sql)
  if(result.err){
   next({
      msg:'稍后再试',
      status:500
    })
    return
  }

  let data={
    msg:"ok",
    status:200,
    result:result.result
  }
 res.send(data)
})


module.exports = router;
